package com.lzb.compressmaster.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.Content;
import com.lzb.compressmaster.HomeActivity;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.adapter.ImgListAdapter;
import com.lzb.compressmaster.databinding.ActivityImg2PdfBinding;
import com.lzb.compressmaster.utils.ImageToPdfUtils;

import java.util.ArrayList;
import java.util.List;

public class Img2PdfActivity extends BaseAc<ActivityImg2PdfBinding> implements View.OnClickListener, OnItemChildClickListener {

    public static List<String> listPath = new ArrayList<>();
    private ImgListAdapter imgListAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_img2_pdf;
    }

    @Override
    protected void initView() {
        mDataBinding.ivImg2PdfBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvImgSort.setOnClickListener(this);
        mDataBinding.tvAdd.setOnClickListener(this);
        mDataBinding.tvComplete.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new LinearLayoutManager(mContext));
        imgListAdapter = new ImgListAdapter();
        mDataBinding.rvPictureList.setAdapter(imgListAdapter);
        imgListAdapter.addChildClickViewIds(R.id.ivImgListItemDelete);
        imgListAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvImgSort) {//图片排序
            ImgSortActivity.type = 0;
            startActivity(ImgSortActivity.class);
        } else if (id == R.id.tvAdd) {//继续添加
            Intent intent = new Intent(this, AddPictureActivity.class);
            intent.putExtra(Content.Again, "Again");
            startActivityForResult(intent, Content.ADD_PICTURE);
        } else if (id == R.id.tvComplete) {//完成
            startConvert();
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        int id = view.getId();
        if (id == R.id.ivImgListItemDelete) {//删除
            listPath.remove(position);
            imgListAdapter.setList(listPath);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        imgListAdapter.setList(listPath);
    }

    private void startConvert() {
        showDialog(getString(R.string.loading));
        ImageToPdfUtils.convertImagesToPdf(mContext, listPath, new ImageToPdfUtils.OnConversionListener() {
            @Override
            public void onConversionStart(int totalImages) {

            }

            @Override
            public void onImageProcessed(int currentIndex, int totalImages, String imagePath) {

            }

            @Override
            public void onConversionComplete(ImageToPdfUtils.ConversionResult result) {
                dismissDialog();
                PdfPreviewActivity.sPath = result.outputPath;
                startActivity(PdfPreviewActivity.class);
            }

            @Override
            public void onConversionError(String errorMessage) {
                dismissDialog();
                ToastUtils.showShort(errorMessage);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            List<String> list = data.getStringArrayListExtra(Content.path);
            if (requestCode == Content.ADD_PICTURE) {
                listPath.addAll(list);
            }
        }
    }

}